package com.ww.java8.fp.stream;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DistinctListDemo {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Student s1 = new Student();
        s1.setId(1);
        s1.setName("张三");
        s1.setAge(20);
        Student s2 = new Student();
        s2.setId(2);
        s2.setName("李四");
        s2.setAge(20);
        Student s3 = new Student();
        s3.setId(1);
        s3.setName("李四");
        s3.setAge(20);
        Student s4 = new Student();
        s4.setId(3);
        s4.setName("李四");
        s4.setAge(20);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        System.out.println(JSON.toJSONString(list));
        List<Student> list2 = new ArrayList<>(list.stream()
                .collect(Collectors.toMap(Student::getId, Function.identity(), (x, y) -> x))
                .values());
        System.out.println(JSON.toJSONString(list2));
    }
}

class Student {

    private Integer id;

    private String name;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}